*Define programs---------------------------------------------------------------;
program drop _all
*Rank vars relative within shina;
program define relrank
	*#1: variable to rank
	*#2: output variable
	sort shina_id `1'
	gen `2'=1 if shina_id!=shina_id[_n-1]
	local n=1
	while `n'<_N{
		local n0=`n'-1
		if shina_id[`n']==shina_id[`n0']{
			if `1'[`n']==`1'[`n0']{
				replace `2'=`2'[`n0'] if _n==`n'
				}
			if `1'[`n']!=`1'[`n0']{
				replace `2'=`2'[`n0']+1 if _n==`n'
				}
		}
		local n=`n'+1
		}
	replace `2'=. if missing(`1')
end

*Rank vars relative within shina-leader
program define relrank_ldr
	*#1: variable to rank
	*#2: output variable
	sort shina_id leader_id `1'
	gen `2'=1 if shina_id!=shina_id[_n-1] | leader_id!=leader_id[_n-1]
	local n=1
	while `n'<_N{
		local n0=`n'-1
		if shina_id[`n']==shina_id[`n0'] & leader_id[`n']==leader_id[`n0']{
			if `1'[`n']==`1'[`n0']{
				replace `2'=`2'[`n0'] if _n==`n'
				}
			if `1'[`n']!=`1'[`n0']{
				replace `2'=`2'[`n0']+1 if _n==`n'
				}
		}
		local n=`n'+1
		}
	replace `2'=. if missing(`1')
end
*=================================================================*
*--------------------Owner & Leader Predictions-------------------*
*=================================================================*
use "${sdir}/Leaders_and_Owners_cln.dta", clear


*Create ranks------------------------------------------------------------------

*within-shina ranking outcomes
relrank s1_q_6_1_own 		rank_inc 		//Income
relrank invoice_value_own 	rank_inv		//Invoice value (respondents)
relrank full_cost_obl 		rank_inv_pop	//Invoice value (population)

*flip leader rankings and create within-shina ranks (so that unobserved respondents not included in ranks)
gen tmp_income=16-q27_ldr 	if owner_observed==1
gen tmp_invoice=16-q28_ldr 	if owner_observed==1
gen rank_inv_pop_ldr=16-q28_ldr

relrank_ldr tmp_income 	rank_inc_ldr
relrank_ldr tmp_invoice rank_inv_ldr
drop tmp_*

*make within plot id
bysort shina_id plot_id: gen within_plot_id=_n


*Rename and relabel------------------------------------------------------------;
rename (s8_q_4_own q37_ldr s1_q_8_3_own q38_ldr)(paidtax paidtax_ldr certsale certsale_ldr)

label var rank_inc 		"\makecell{Income\\Rank}"
label var rank_inv 		"\makecell{Invoice\\Rank}"
label var rank_inv_pop 	"\makecell{Invoice\\Rank\\Full}"
label var paidtax 		"\makecell{Property\\Tax\\Paid}"
label var certsale 		"\makecell{Certificate\\of\\Sale}"

*Regressions-------------------------------------------------------------------
local reg_lst rank_inc rank_inv rank_inv_pop paidtax certsale
local controls_lst gender_ldr q9_1_ldr q4_1_ldr surveyor_id_ldr 

quietly{
local c=0
foreach v in `reg_lst'{
	local c=`c'+1
	preserve
	gen temp=`v'_ldr
	
	*get mean outcome
	reg `v' if within_plot_id==1 & missing(temp)==0
	local meanC=_b[_cons]
	local meanCse=_se[_cons]
	local meanCdi: di %3.2f `meanC'
	local meanCsedi: di %4.3f `meanCse'
	
	*Panel A: Predictions
	eststo pnl1_`c': reghdfe `v' temp, vce(cluster shina_leader_id) absorb(strata trt_grp_ldr)
	local wR2di: di %4.3f e(r2_a_within)
	estadd local meanC="`meanCdi'"
	estadd local wR2="`wR2di'"
	
	*Panel B: within shina
	eststo pnl2_`c': reghdfe `v' temp, vce(cluster shina_leader_id) absorb(strata trt_grp_ldr shina_id)
	local wR2di: di %4.3f e(r2_a_within)
	estadd local wR2="`wR2di'"
	
	*Panel C: within shina + controls
	eststo pnl3_`c': reghdfe `v' temp, vce(cluster shina_leader_id) absorb(strata trt_grp_ldr shina_id `controls_lst')
	local wR2di: di %4.3f e(r2_a_within)
	estadd local wR2="`wR2di'"
	
	restore 
	}

}

*write to file
local note ///
	This Table presents coefficients from regressions of standard plot characteristics on leader's predicitons of those characteristics. Each observation is a leader-plot pair. Column 1 the dependent variable is the within neighbourhood rank of plot owner's income. The dependent variable in columns 2 and 3 is the within neighbourhood rank of invoice value. Column 2 restricts the sample to respondent owners, while column 3 includes all invoices. Column 4 the dependent variable is an indicator if the plot owner reported paying property tax in 2018. Column 5 the dependent variable is an indicator if the plot owner reported having a certificate of sale. Leader treatment group and randomization strata fixed effects are always included as controls. Each panel represents a different set of controls: panel A has no additional controls, panel B adds shina (neighbourhood) fixed effects, and panel C further adds a set of controls (leader gender, leader household income bracket, a dummy for leader's home plot being surveyed, and fixed effects for the ID of the leader's surveyor). Standard errors in parentheses are clustered at leader level. \sym{*}\(p<0.10\),	\sym{**}\(p<0.05\), \sym{***}\(p<0.01\).
		
*put all panels together in one table
//top panel 
esttab pnl1* using "tables/standard_chars.tex", ///
	keep(temp) ///
	coeflabels(temp "Leader Prediction") ///
	stats(meanC N r2 wR2, label("Mean outcome" "Observations" "R$^2$" "within-R$^2$" )) ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	se label wrap  nocons fragment ///
	prehead("\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} {\centering\begin{tabular}{l*{5}{c}} \hline\hline") ///
	posthead("\hline \\ \multicolumn{4}{l}{\textit{Panel A: Predictions}} \\\\[-1ex]") ///
	replace 

//middle panel #1
esttab pnl2* using "tables/standard_chars.tex", ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	coeflabels(temp "Leader Prediction") ///
	stats(N r2 wR2, label("Observations" "R$^2$" "within-R$^2$")) ///
	se label wrap fragment ///
	nomtitles nonumbers ///
	nobaselevels nocons ///
	posthead("\hline \\ \multicolumn{5}{l}{\textit{Panel B: + controls for neighbourhood FE}} \\\\[-1ex]") ///
	append

//bottom panel 
esttab pnl3* using "tables/standard_chars.tex", ///
	keep(temp) ///
	coeflabels(temp "Leader Prediction") ///
	stats(N r2 wR2, label("Observations" "R$^2$" "within-R$^2$" )) ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	se label wrap  nocons fragment ///
	nomtitles nonumbers ///
	posthead("\hline \\ \multicolumn{5}{l}{\textit{Panel C: + leader characteristic controls}} \\\\[-1ex]") ///
	prefoot("\hline") ///
	postfoot("\hline\hline \end{tabular}\par\medskip}{\footnotesize {\it Note: }`note'}") ///
	append
estimates clear




*END--------
clear
